$(function () {
    doSpoor();
})

var page = 0,
    spoorNum = 0,
    token = getItem('token');
function doSpoor() {
    page++;
    $.ajax({
        url:'/footMarkC/myFootMark.json',
        dataType:'json',
        data:{
            pageNu:page,
            token:token,
            companyCode:getItem('companyCode'),
        },
        type:'post',
        beforeSend:function(){
            if(page==1){
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.list_box'));
            }
        },
        success:function (data) {
            if(data.code==1) {
                if (page == 1) {
                    $('.list_box').empty();
                    if (data.datas.footMarkList.length == 0) {
                        $('#clear').hide();
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../phoneImg/kong.png" alt="暂无数据"></div>').appendTo($('.list_box'));
                    }
                }
                var spoorData = data.datas.footMarkList;
                for(var i=0;i<spoorData.length;i++){
                    var mainImg = spoorData[i].mainImage.split(',');
                    $('<div class="item_box mui-clearfix">\n' +
                        '<div class="img" goodsId="'+spoorData[i].goodsId+'"><img src="'+mainImg[0]+'" alt=""></div>\n' +
                        '<div class="goods" goodsId="'+spoorData[i].goodsId+'">\n' +
                        '<div class="title" goodsId="'+spoorData[i].goodsId+'">'+spoorData[i].name+'</div>\n' +
                        '<div class="price" goodsId="'+spoorData[i].goodsId+'"><b>￥</b>'+spoorData[i].salesPrice+'</div>\n' +
                        '<div class="comment" id="'+spoorData[i].id+'">删除</div>\n' +
                        '</div></div>').appendTo($('.list_box'));
                }
                //跳转
                mui('body').on('tap','.img,.title,.price',function () {
                    window.location.href="goods.html?goodsId="+$(this).attr('goodsId');
                })
                spoorNum = spoorData.length;
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
        }
    })
    return spoorNum;
}

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doSpoor();
                    if (numResult < 10) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});

//删除
mui('body').on('tap','.comment',function () {
    var delId = $(this).attr('id');
    var tThis = $(this);
    var btnArray = ['取消', '确定'];
    mui.confirm('您确定要删除此足迹吗？', '', btnArray, function(e) {
        if (e.index == 1) {
            $.ajax({
                url:'/footMarkC/delFootMark.json',
                dataType:'json',
                data:{
                    token:getItem('token'),
                    idStr:delId,
                    companyCode:getItem('companyCode'),
                },
                type:'post',
                beforeSend:function(){
                    $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
                },
                success:function (data) {
                    $('.loading2').hide();
                    if(data.code==1){
                        mui.toast('删除成功')
                        tThis.parent().parent().remove();
                    }
                    if(data.code==0){
                        mui.toast(data.msg);
                    }
                    if(data.code==-1){
                        window.location.href="login.html"
                    }
                }
            })
        } else {

        }
    })
})


//清空
document.getElementById('clear').addEventListener('tap',function () {
    $.ajax({
        url:'/footMarkC/cleanFootMarK.json',
        dataType:'json',
        data:{
            token:token,
            companyCode:getItem('companyCode'),
        },
        type:'post',
        beforeSend:function(){
            $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
        },
        success:function (data) {
            $('.loading2').hide();
            if (data.code == 1) {
                $('.list_box').empty();
                mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                $('<div class="kong"><img src="../phoneImg/kong.png" alt="暂无数据"></div>').appendTo($('.list_box'));
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
        }
    })
})
